#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Boot_RoutineControl_0x31_test.py
@Time    :   2024/09/11 13:31:28
@Author  :   Yangliang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume

import pytest
import time
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

RoutinControl_NRC = [   ([0x31], [0x7F, 0x31, 0x13]),  # NRC13
                        ([0x31, 0x01], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x02], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x03], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x01, 0x02], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x02, 0x02], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x01, 0xFF], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x01, 0x02, 0x02, 0x03, 0x00], [0x7F, 0x31, 0x13]),  # 刷写RID_0202 长度为68字节
                        ([0x31, 0x01, 0xFF, 0x01, 0x00], [0x7F, 0x31, 0x13]),
                        ([0x31, 0x7F, 0x02, 0x02], [0x7F, 0x31, 0x12]),
                        ([0x31, 0x00, 0xFF, 0x01], [0x7F, 0x31, 0x12]),
                        ([0x31, 0x04, 0xFF, 0x01], [0x7F, 0x31, 0x12]),
                        ([0x31, 0x7F, 0xFF, 0x01], [0x7F, 0x31, 0x12]),
                        ([0x31, 0x01, 0x01, 0x22], [0x7F, 0x31, 0x31]),  # NRC31
                        ([0x31, 0x02, 0x01, 0x22], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x03, 0x01, 0x22], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x01, 0x02, 0x03], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x01, 0x12, 0x7A], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x02, 0x12, 0x7A], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x03, 0x12, 0x7A], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x01, 0x12, 0x7B], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x02, 0x12, 0x7B], [0x7F, 0x31, 0x31]),
                        ([0x31, 0x03, 0x12, 0x7B], [0x7F, 0x31, 0x31])
                    ]

RoutinControl = [   ([0x31, 0x01, 0xFF, 0x01], [0x71, 0x01, 0xFF, 0x01, 0x00]),
                    ([0x31, 0x01, 0x02, 0x02], [0x7F, 0x31, 0x22]),
                ]

class Test_RoutineControl_Phy(object):

    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in RoutinControl]
    @allure.severity('normal')
    @allure.story('Routine conteol test')
    @allure.description('Test positive response of 0x31 RID in programming session with physical addressing')
    @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', RoutinControl, ids=ids)
    def test_RoutineControl_DID_Phy(self, action, Driver, req, resp,):
        iStep = 1
        if req == [0x31, 0x01, 0x02, 0x02]:
            for i in range(256):
                req.append(0x00)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True, unlock_level = 3)
    

    ids = ["Request {0}".format(binascii.b2a_hex(bytearray(req), ' '))for req, resp in RoutinControl_NRC]
    @allure.severity('minor')
    @allure.description('Test NRC12,NRC13,NRC31 of 0x31 in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    @pytest.mark.parametrize('req,resp', RoutinControl_NRC, ids=ids)
    def test_RoutineControl_NRC_Phy(self, action, Driver, req, resp):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 2, iStep, True)
        

    @allure.severity('minor')
    @allure.description('Test NRC7F of 0x31 in default session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_RoutineControl_NRC7F_Default_Phy(self, action, Driver):
        iStep = 1
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x31, 0x01, 0x02, 0x02, 0x01, 0xAB, 0xE3, 0x6F], [0x7F, 0x31, 0x7F], 1, iStep, False)

    @allure.severity('minor')
    @allure.description('Test NRC33 of 0x31 in programming session with physical addressing')
    @allure.testcase('Test cases address:https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
    def test_RoutineControl_NRC33_Phy(self, action, Driver):
        iStep = 1
        req = [0x31, 0x01, 0x02, 0x02]
        for i in range(256):
            req.append(0x00)
        iStep, uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [0x7F, 0x31, 0x33], 2, iStep, False)

